package com.xzx.paymentdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.xzx.paymentdemo.entity.OrderInfo;
import com.xzx.paymentdemo.entity.RefundInfo;
import com.xzx.paymentdemo.enums.OrderStatus;
import com.xzx.paymentdemo.enums.wxpay.WxRefundStatus;
import com.xzx.paymentdemo.mapper.RefundInfoMapper;
import com.xzx.paymentdemo.service.OrderInfoService;
import com.xzx.paymentdemo.service.RefundInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzx.paymentdemo.util.OrderNoUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {


    @Resource
    private OrderInfoService orderInfoService;

    /**
     * 创建退款订单
     * @param orderNo
     * @param reason
     * @return
     */
    @Override
    public RefundInfo createRefundByOrderNo(String orderNo, String reason) {
        OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderNo);
        RefundInfo refundInfo = new RefundInfo();
        refundInfo.setOrderNo(orderNo)
                .setRefundNo(OrderNoUtils.getRefundNo())
                .setTotalFee(orderInfo.getTotalFee())//原金额
                .setRefund(orderInfo.getTotalFee())//退款金额
                .setReason(reason);

        save(refundInfo);
        return refundInfo;
    }

    /**
     * 记录退款记录
     * @param content
     */
    @Override
    public void updateRefund(String content) {
        //将json字符串转换成Map
        Gson gson = new Gson();
        Map<String, String> resultMap = gson.fromJson(content, HashMap.class);
        //根据退款单编号修改退款单
        LambdaQueryWrapper<RefundInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RefundInfo::getRefundNo, resultMap.get("out_refund_no"));

        RefundInfo refundInfo = new RefundInfo();
        refundInfo.setRefundId(resultMap.get("refund_id"));
        //返回参数和回调参数的退款单的状态字段不同，根据这个来区分
        //1.响应体：查询退款和申请退款中的返回参数
        if (resultMap.get("status") != null) {
            refundInfo.setRefundStatus(resultMap.get("status"));
            refundInfo.setContentReturn(content);
        }
        //2.请求体：退款回调中的回调参数
        if (resultMap.get("refund_status") != null) {
            refundInfo.setRefundStatus(resultMap.get("refund_status"));
            refundInfo.setContentNotify(content);
        }
        update(refundInfo,wrapper);
    }

    /**
     * 找出申请退款超过minutes分钟并且未成功的退款单
     * @param minutes
     * @return
     */
    @Override
    public List<RefundInfo> getNoPayOrderByDurations(int minutes) {
        //minutes分钟之前的时间
        Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));
        List<RefundInfo> RefundInfoList = lambdaQuery().eq(RefundInfo::getRefundStatus, WxRefundStatus.PROCESSING.getType())
                .le(RefundInfo::getCreateTime, instant)
                .list();
        return RefundInfoList;
    }
}
